package com.itzdm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itzdm.reggie.common.constant.MessageConstant;
import com.itzdm.reggie.common.exception.DeletionNotAllowedException;
import com.itzdm.reggie.dto.SetmealDto;
import com.itzdm.reggie.entity.Category;
import com.itzdm.reggie.entity.Setmeal;
import com.itzdm.reggie.entity.SetmealDish;
import com.itzdm.reggie.mapper.SetmealMapper;
import com.itzdm.reggie.service.CategoryService;
import com.itzdm.reggie.service.SetmealDishService;
import com.itzdm.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 新增套餐以及添加对应的菜品
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐表
        save(setmealDto);
        //添加套餐菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).toList();
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page pageSet(int page, int pageSize, String name) {
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> p = new Page<>();
        //添加查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        page(pageInfo,wrapper);
        //拷贝属性
        BeanUtils.copyProperties(pageInfo,p,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).toList();
        p.setRecords(list);
        return p;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //判断套餐是否正在起售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        wrapper.eq(Setmeal::getStatus,1);
        long count = count(wrapper);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapperDish = new LambdaQueryWrapper<>();
        wrapperDish.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapperDish);

    }

    /**
     * 启用禁用套餐
     * @param status
     * @param ids
     */
    @Override
    public void startOrStop(Integer status, List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new DeletionNotAllowedException("没有选中套餐");
        }
        for (Long id : ids) {
            Setmeal setmeal = getById(id);
            setmeal.setStatus(status);
            updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        return list(wrapper);
    }
}
